﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChanExt;
using C.Entities;


namespace C.Unite
{
    /// <summary>
    /// 窗体属性
    /// </summary>
    public class WindowAttribute
    {
        /// <summary>
        /// 宽
        /// </summary>
        public int Width { get; set; }
        /// <summary>
        /// 高
        /// </summary>
        public int Height { get; set; }
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }
    }

    /// <summary>
    /// FORM信息
    /// </summary>
    public class FormInfo
    {
        /// <summary>
        /// 弹出选择窗口
        /// </summary>
        /// <param name="window">窗口</param>
        /// <param name="firstBox">第一个主控件</param>
        /// <param name="secondBox">副控件</param>
        /// <param name="Page">跳转页面</param>
        /// <param name="WinTitle">窗体标题</param>
        public static void ShowTriggerWin(Window window, TriggerBox firstBox, HiddenField secondBox, string Page, string WinTitle)
        {
            firstBox.OnClientTriggerClick = window.GetSaveStateReference(secondBox.ClientID, firstBox.ClientID)
                     + window.GetShowReference(Page, WinTitle);
        }
        public static void ShowTriggerWin(Window window, TriggerBox firstBox, TextBox secondBox, string Page, string WinTitle)
        {
            firstBox.OnClientTriggerClick = window.GetSaveStateReference(secondBox.ClientID, firstBox.ClientID)
                    + window.GetShowReference(Page, WinTitle);
        }
        /// <summary>
        /// 弹出选择窗口
        /// </summary>
        /// <param name="window">窗口</param>
        /// <param name="firstBox">第一个主控件</param>
        /// <param name="secondBox">副控件</param>
        /// <param name="secondBox">副控件2</param>
        /// <param name="Page">跳转页面</param>
        /// <param name="WinTitle">窗体标题</param>
        public static void ShowTriggerWin(Window window, TriggerBox firstBox, HiddenField secondBox, Label thirBox, string Page, string WinTitle)
        {
            firstBox.OnClientTriggerClick = window.GetSaveStateReference(secondBox.ClientID, firstBox.ClientID, thirBox.ClientID)
                     + window.GetShowReference(Page, WinTitle);
        }

        public static void TabView(string page, string tabname, string icon, string tabtitle)
        {
            string tab = "TabView('" + page + "', 'tab_" + tabname + "','" + tabtitle + "', '" + icon + "')";
            PageContext.RegisterStartupScript(tab);
        }
        /// <summary>
        /// GRIDTAB查看
        /// </summary>
        /// <param name="gd">GRID控件</param>
        /// <param name="page">链接的页面地址</param>
        /// <param name="parmname">参数名</param>
        /// <param name="icon">图标</param>
        /// <param name="tabtitle">选项卡标题</param>
        public static void GridView(Grid gd, string page, string pam, string icon, string tabtitle)
        {
            int[] list = gd.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                if (list.Length > 1)
                {
                    Alert.Show("请选择一项进行查看！");
                }
                else
                {
                    int index = list[0];
                    int id = int.Parse(gd.DataKeys[index][0].ToString());
                    int c = new Random().Next();
                    page = page + "?" + pam + "=" + id;
                    string tab = "TabView('" + page + "', 'tab_" + c + "_" + id + "','" + tabtitle + "', '" + icon + "')";
                    PageContext.RegisterStartupScript(tab);
                }
            }
            else
                Alert.Show("请选择一项进行查看！");
        }

        /// <summary>
        /// 弹出选项卡
        /// </summary>
        /// <param name="page">链接页面</param>
        /// <param name="tabname">选项卡标识</param>
        /// <param name="icon">图标</param>
        /// <param name="tabtitle">选项卡标题</param>
        public static void ShowTab(string page, string tabname, string icon, string tabtitle)
        {
            string tab = "TabView('" + page + "', 'tab_" + tabname + "','" + tabtitle + "', '" + icon + "')";
            PageContext.RegisterStartupScript(tab);
        }

        /// <summary>
        /// 修改事件
        /// </summary>
        /// <param name="gd">GRID控件</param>
        /// <param name="w1">窗体控件</param>
        /// <param name="page">跳转的页面</param>
        /// <param name="wa">窗体属性</param>
        public static void GridEdit(Grid gd, Window w1, string page, WindowAttribute wa, string parmname)
        {
            string par = "id";
            if (parmname != "")
                par = parmname;
            string title = "修改";
            if (wa != null)
            {
                title = wa.Title;
                w1.Title = wa.Title;
                w1.Width = System.Web.UI.WebControls.Unit.Pixel(wa.Width);
                w1.Height = System.Web.UI.WebControls.Unit.Pixel(wa.Height);
            }
            int[] list = gd.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                if (list.Length > 1)
                {
                    Alert.Show("请选择一项进行修改！");
                }
                else
                {
                    int index = list[0];
                    int id = int.Parse(gd.DataKeys[index][0].ToString());
                    string url = "";
                    if (page.Contains("?"))
                        url = page + "&" + par + "=" + id;
                    else
                        url = page + "?" + par + "=" + id;


                    PageContext.RegisterStartupScript(w1.GetShowReference(url, title));
                }
            }
            else
                Alert.Show("请选择一项进行修改！");
        }


        public static string GetGridSelected(Grid gd, int tindex)
        {
            string n = "";
            int[] list = gd.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                n = gd.DataKeys[list[0]][tindex].ToString();
            }
            return n;
        }

        /// <summary>
        /// 修改事件
        /// </summary>
        /// <param name="gd">GRID</param>
        /// <param name="w1">WINDOW</param>
        public static void GridEdit(Grid gd, Window w1, string page)
        {
            GridEdit(gd, w1, page, null, "");
        }
        /// <summary>
        /// 删除事件
        /// </summary>
        /// <typeparam name="T">实体类</typeparam>
        /// <param name="gd">GRID</param>
        /// <param name="w1">WINDOW</param>
        /// <param name="t">实体类</param>
        public static void GridDelete<T>(Grid gd, Window w1, T t) where T : MySoft.Data.Entity
        {
            int[] list = gd.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                int del = 0;
                foreach (int item in list)
                {
                    int index = item;
                    int id = int.Parse(gd.DataKeys[index][0].ToString());
                    string s = t.GetIdentityField().FieldName.Replace("{0}", "").Replace("{1}", "");
                    string tablename = t.GetTableName();
                    del += TbsDbSession.Default.FromSql("delete from " + tablename + " where " + s + " = " + id).ExecuteNonQuery();
                }
                if (del > 0)
                    Alert.Show("删除成功！");
            }
            else
                Alert.Show("请选择一项进行删除！");
        }




        public static void DataBaseBind(string GroupNo, DropDownList dl)
        {
            List<Sys_BaseData> baseData = TbsDbSession.Default.From<Sys_BaseData>().Where(Sys_BaseData._.GroupNo == GroupNo.Trim()).OrderBy(Sys_BaseData._.OrderID.Asc, Sys_BaseData._.ID.Desc).ToList();
            dl.DataSource = baseData;
            dl.DataTextField = "BaseName";
            dl.DataValueField = "BaseName";
            dl.DataBind();
        }
        /// <summary>
        /// 基础数据绑定
        /// </summary>
        /// <param name="GroupID"></param>
        /// <param name="dl"></param>
        public static void DataBaseBind(int GroupID, DropDownList dl)
        {
            List<Sys_BaseData> baseData = TbsDbSession.Default.From<Sys_BaseData>().Where(Sys_BaseData._.GroupID == GroupID).OrderBy(Sys_BaseData._.OrderID.Asc, Sys_BaseData._.ID.Desc).ToList();
            dl.DataSource = baseData;
            dl.DataTextField = "BaseName";
            dl.DataValueField = "BaseName";
            dl.DataBind();
        }



        public static void DepartMentBind(DropDownList DrpList)
        {
            int c = 0;
            List<OA_Department> _mentList = TbsDbSession.Default.From<OA_Department>().OrderBy(OA_Department._.OrderID.Asc, OA_Department._.DepID.Desc).ToList();
            List<OA_Department> items = _mentList.FindAll(delegate(OA_Department tas) { return tas.TopID == 0; });// TbsDbSession.Default.From<GS_ArticleSort>().Where(GS_ArticleSort._.FatherID == 0).OrderBy(GS_ArticleSort._.ShowNo.Asc).ToList();
            foreach (OA_Department row in items)
            {
                ListItem li = new ListItem();
                li.Text = row.DepName;
                li.Value = row.DepID.ToString();
                li.SimulateTreeLevel = 0;
                li.EnableSelect = false;
                DrpList.Items.Add(li);
                ResolveSubTree(row.DepID, DrpList, _mentList, c + 1);
            }
            DrpList.Items.Insert(0, new ListItem("--请选择部门--", "0"));
        }

        private static void ResolveSubTree(int id, DropDownList treeNode, List<OA_Department> mentList, int lev)
        {
            List<OA_Department> items = mentList.FindAll(delegate(OA_Department tas) { return tas.TopID == id; });// TbsDbSession.Default.From<GS_ArticleSort>().Where(GS_ArticleSort._.FatherID == 0).OrderBy(GS_ArticleSort._.ShowNo.Asc).ToList();
            foreach (OA_Department row in items)
            {
                ListItem li = new ListItem();
                li.Text = row.DepName;
                li.Value = row.DepID.ToString();
                li.SimulateTreeLevel = lev;
                treeNode.Items.Add(li);
                int c = mentList.FindAll(p => p.TopID == row.DepID).Count;
                if (c > 0)
                {
                    lev++;
                    ResolveSubTree(row.DepID, treeNode, mentList, lev);
                    lev = 1;
                }
            }
        }


    }

}
